function addStaff() {
    $('#add-staff-form').submit();
}

$(document).ready(function () {

        $('#add-staff-form').submit(function ()//提交表单
        {
            var options = {
                url: '/admin/addStaff', //提交给哪个执行
                type: 'POST',
                success: function (data) {
                    alert("添加成功！");
                    $("#staff-list").prepend(data);
                    $("#add-staff-dialog").modal("hide");
                } //显示操作提示
            };
            $('#add-staff-form').ajaxSubmit(options);
            return false; //为了不刷新页面,返回false，反正都已经在后台执行完了，没事！

        });

        $('#update-staff-info-form').submit(function ()//提交表单
        {
            var options = {
                url: '/admin/updateStaffInfo', //提交给哪个执行
                type: 'POST',
                success: function (data) {
                    alert("修改成功！");
                    //$("#staff-list").prepend(data);

                    var newInfo = data.data;

                    $("#staff-data-" + toUpdateStaffId).attr("data", "(" + JSON.stringify(newInfo) + ")");

                    var oldInfoDivs = $("#staff-" + toUpdateStaffId + " > div");

                    oldInfoDivs.eq(1).html(newInfo.name);

                    oldInfoDivs.eq(2).html(newInfo.type == 0 ? "管理员" : "红娘");

                    //$("#update-staff-type-"+type).attr("checked",true);
                    $("#update-staff-info-dialog").modal("hide");
                } //显示操作提示
            };
            $('#update-staff-info-form').ajaxSubmit(options);
            return false; //为了不刷新页面,返回false，反正都已经在后台执行完了，没事！

        });
        $("#staff-list").css("height", getClientHeight() - 212).css("maxHeight", getClientHeight() - 120);

        $(window).resize(function () {
            $("#staff-list").css("height", getClientHeight() - 212).css("maxHeight", getClientHeight() - 120);
            $("#rightContent").css("width", $(window).width() - 120);
        });

        $("#staff-page-select").change(function () {

            var page=$(this).children('option:selected').val()-1;

            staffListTurnPage(page);
        });

        getStaffCount();
        staffListTurnPage(0);
    }
);

var toDeleteStaffId;

function beforeDeleteStaff(staffId) {
    toDeleteStaffId = staffId;
}

function deleteStaff() {
    $.get("/admin/deleteStaff?" + "staffId=" + toDeleteStaffId, function (data) {


        //alert(data.code);

        if (data.code === 1) {
            alert("删除成功！");
            $("#staff-" + toDeleteStaffId).remove();
        }
        else {
            alert("删除失败！");

        }

        $("#deleteStaffAlert").modal('hide');
        // $("#staff-list").empty();
        // $("#staff-list").append(data);
    });
}

function staffPreviousPage() {
    staffListTurnPage(staffCurrentPage-1);
    $("#staff-page-select").val(staffCurrentPage)
}

function staffNextPage() {
    staffListTurnPage(staffCurrentPage+1);
    $("#staff-page-select").val(staffCurrentPage+2);
}

function staffFirstPage() {
    staffListTurnPage(0);
    $("#staff-page-select").val(1);
}

function staffLastPage() {
    staffListTurnPage(staffTotalPage-1);
    $("#staff-page-select").val(staffTotalPage);
}


var staffTotalPage;


function getStaffCount() {
    $.get("/admin/getStaffCount" , function (data) {
        var pageSelect=$("#staff-page-select");
        pageSelect.empty();
        staffTotalPage=parseInt((data.data-1)/20+1);
        $("#staff-total-page").html(staffTotalPage);
        for (var i=0;i<staffTotalPage;i++){
            pageSelect.append("<option>"+(i+1)+"</option>");
        }
        //staffCurrentPage = page;
    });
}

var staffCurrentPage = 0;

function staffListTurnPage(page) {
    $.get("/admin/getStaffList?" + "offset=" + page * 20 + "&count=20", function (data) {
        $("#staff-list").empty();
        $("#staff-list").append(data);

        staffCurrentPage = page;

        if (staffCurrentPage==0){
            $("#staff-previous-page").attr("disabled","disabled");
            $("#staff-first-page").attr("disabled","disabled");
        }
        else {
            $("#staff-previous-page").removeAttr("disabled");
            $("#staff-first-page").removeAttr("disabled");
        }

        if (staffCurrentPage>=staffTotalPage-1){
            $("#staff-next-page").attr("disabled","disabled");
            $("#staff-last-page").attr("disabled","disabled");
        }
        else {
            $("#staff-next-page").removeAttr("disabled");
            $("#staff-last-page").removeAttr("disabled");
        }

    });
}

var toUpdateStaffId;

function beforeUpdateStaffInfo(staffId) {
    toUpdateStaffId = staffId;

    var info = eval($("#staff-data-" + staffId).attr("data"));

    $("#update-staff-email").val(info.email);
    $("#update-staff-id").val(staffId);
    $("#update-staff-name").val(info.name);
    $("#update-staff-type-" + info.type).attr("checked", true);
    $("#update-staff-gender-" + (info.gender == "男" ? 0 : 1)).attr("checked", true);

}

function updateStaffInfo() {
    $('#update-staff-info-form').submit();
}